package com.ysxsoft.wdg.ui.activity.home;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.classic.common.MultipleStatusView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.adapter.TabAdapter;
import com.ysxsoft.wdg.adapter.home.JxGoodsAdapter;
import com.ysxsoft.wdg.adapter.home.ZyGoodsAdapter;
import com.ysxsoft.wdg.base.BaseActivity;
import com.ysxsoft.wdg.bean.BannerBean;
import com.ysxsoft.wdg.bean.response.HomeProductResponse;
import com.ysxsoft.wdg.bean.zy.GoodsBean;
import com.ysxsoft.wdg.net.CallbackCode;
import com.ysxsoft.wdg.net.UrlHost;
import com.ysxsoft.wdg.ui.activity.login.LoginValid;
import com.ysxsoft.wdg.util.AlibcUtils;
import com.ysxsoft.wdg.util.BigDecimalUtil;
import com.ysxsoft.wdg.util.JsonUtils;
import com.ysxsoft.wdg.util.ViewUtils;
import com.ysxsoft.wdg.util.delayaction.Action;
import com.ysxsoft.wdg.util.delayaction.SingleCall;
import com.ysxsoft.wdg.util.sp.SharePrefUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * 购物精选
 */
public class TbJxActivity extends BaseActivity {


    public static void start(Context context) {
        Intent intent = new Intent(context, TbJxActivity.class);
        context.startActivity(intent);
    }

    @BindView(R.id.topView)
    View topView;
    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.title_iv_r)
    ImageView titleIvR;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.tv0)
    TextView tv0;
    @BindView(R.id.tv1)
    TextView tv1;
    @BindView(R.id.tv2)
    TextView tv2;
    @BindView(R.id.tv3)
    TextView tv3;
    @BindView(R.id.iv3)
    ImageView iv3;
    @BindView(R.id.recyclerview2)
    RecyclerView recyclerview2;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.multStatusView)
    MultipleStatusView multStatusView;

    private TextView[] textViews;

    private TabAdapter tabAdapter;
    private JxGoodsAdapter adapter;
    private int page = 1;
    private int type=0;//综合0，销量1，最新2，到手价倒序3， 到手价正序4
    private String title = "全部";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_tb_jx;
    }

    @Override
    protected void initData() {
        initStatusBar(topView, false);
        titleContent.setText("购物精选");

        titleIvR.setImageResource(R.mipmap.zy_search);
        textViews = new TextView[]{tv0, tv1, tv2, tv3};
        setSelect(0);

        recyclerview.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.HORIZONTAL));
        tabAdapter = new TabAdapter(R.layout.item_tab);
        recyclerview.setAdapter(tabAdapter);
        initCategory();

        recyclerview2.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        adapter = new JxGoodsAdapter(R.layout.item_goods);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                SingleCall.getInstance().addAction(new Action() {
                    @Override
                    public void call() {
                        HomeProductResponse.DataBean item = (HomeProductResponse.DataBean) adapter.getItem(position);
                        AlibcUtils.showDetail(TbJxActivity.this, item.getNum_iid(), SharePrefUtils.getUserId());
                    }
                }).addValid(new LoginValid(mContext, 1)).doCall();
            }
        });
        recyclerview2.setAdapter(adapter);

        getProductList();
    }


    @Override
    protected void setListener() {
        tabAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                tabAdapter.setSelect(position);
                BannerBean bannerBean = (BannerBean) adapter.getItem(position);
                title = bannerBean.getTitle();
                page=1;
                getProductList();
            }
        });
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                getProductList();
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page++;
                getProductList();
            }
        });
    }

    @OnClick({R.id.title_finish, R.id.title_iv_r, R.id.tv0, R.id.tv1, R.id.tv2, R.id.tv3})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_finish:
                finish();
                break;
            case R.id.title_iv_r://跳转至淘宝搜索
                TbSearchActivity.start(TbJxActivity.this);
                break;
            case R.id.tv0:
                type = 0;
                setSelect(0);
                break;
            case R.id.tv1:
                type = 1;
                setSelect(1);
                break;
            case R.id.tv2:
                type = 2;
                setSelect(2);
                break;
            case R.id.tv3:
                type = (type == 3) ? 4 : 3;
                setSelect(3);
                iv3.setImageResource((type == 3) ? R.mipmap.xu_down : R.mipmap.xu_up);
                break;
        }
    }

    private void setSelect(int position) {
        for (int i = 0; i < textViews.length; i++) {
            textViews[i].setSelected(i == position);
        }
        page=1;
        getProductList();
    }

    private void initCategory() {
//        男装  女装  内衣  美妆  居家  母婴  食品  数码   电器  文体  户外
        String[] s = new String[]{"全部", "男装", "女装", "内衣", "美妆", "居家", "母婴", "食品", "数码", "电器", "文体", "户外"};
        List<BannerBean> tabs = new ArrayList<>();
        for (int i = 0; i < s.length; i++) {
            BannerBean tabCategory = new BannerBean();
            if (i == 0) {
                tabCategory.setSelect(true);
            } else {
                tabCategory.setSelect(false);
            }
            tabCategory.setTitle(s[i]);
            tabs.add(tabCategory);
        }
        tabAdapter.setNewData(tabs);
    }

    /**
     * 获取购物精选数据
     */
    private void getProductList() {
        if (page == 1) {
            multStatusView.showLoading();
        }
        OkHttpUtils.post()
                .url(UrlHost.HOME_PRODUCT)
                .addParams("uid",  SharePrefUtils.getUserId())
                .addParams("page", page + "")
                .addParams("type", "" + type)//	综合0，销量1，最新2，到手价倒序3，到手价正序4
                .addParams("keyword", title)
                .tag(this)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (!call.isCanceled()) {
                    multStatusView.showError();
                    reset();
                }
            }

            @Override
            public void onResponse(String response, int id) {
                reset();
                if (response != null) {
                    HomeProductResponse homeProductResponse = JsonUtils.parseByGson(response, HomeProductResponse.class);
                    if (homeProductResponse == null) {
                        return;
                    }
                    if (CallbackCode.SUCCESS.equals(homeProductResponse.getCode())) {
                        List<HomeProductResponse.DataBean> list = homeProductResponse.getData();
                        if (page == 1) {
                            multStatusView.showContent();
                            if (list != null && !list.isEmpty()) {
                            } else {
                                multStatusView.showEmpty();
                            }
                            adapter.setNewData(list);
                        } else {
                            adapter.addData(list);
                        }
                    }
                } else {
                    multStatusView.showError();
                }
            }
        });
    }

    private void reset() {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.finishRefresh(true);
        }
        refreshLayout.finishLoadmore();
    }

}
